package sdk.webview.fmc.com.fmcsdk.imagepicker.utils;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.WindowManager;

import androidx.annotation.NonNull;

import java.io.File;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import sdk.webview.fmc.com.fmcsdk.R;
import sdk.webview.fmc.com.fmcsdk.check.bean.TypeSelectBean;

/**
 * 屏幕相关工具类
 */
public class Utils {

    /**
     * 获取屏幕的宽和高
     *
     * @param context
     * @return
     */
    public static int[] getScreenSize(Context context) {
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics displayMetrics = new DisplayMetrics();
        windowManager.getDefaultDisplay().getMetrics(displayMetrics);
        return new int[]{displayMetrics.widthPixels, displayMetrics.heightPixels};
    }

    /**
     * 获取图片格式化时间
     *
     * @param timestamp
     * @return
     */
    public static String getImageTime(long timestamp) {
        Calendar currentCalendar = Calendar.getInstance();
        currentCalendar.setTime(new Date());
        Calendar imageCalendar = Calendar.getInstance();
        imageCalendar.setTimeInMillis(timestamp);
        if (currentCalendar.get(Calendar.DAY_OF_YEAR) == imageCalendar.get(Calendar.DAY_OF_YEAR) && currentCalendar.get(Calendar.YEAR) == imageCalendar.get(Calendar.YEAR)) {
            return "今天";
        } else if (currentCalendar.get(Calendar.WEEK_OF_YEAR) == imageCalendar.get(Calendar.WEEK_OF_YEAR) && currentCalendar.get(Calendar.YEAR) == imageCalendar.get(Calendar.YEAR)) {
            return "本周";
        } else if (currentCalendar.get(Calendar.MONTH) == imageCalendar.get(Calendar.MONTH) && currentCalendar.get(Calendar.YEAR) == imageCalendar.get(Calendar.YEAR)) {
            return "本月";
        } else {
            Date date = new Date(timestamp);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM");
            return sdf.format(date);
        }
    }

    /**
     * 获取视频时长（格式化）
     *
     * @param timestamp
     * @return
     */
    public static String getVideoDuration(long timestamp) {
        if (timestamp < 1000) {
            return "00:01";
        }
        Date date = new Date(timestamp);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("mm:ss");
        return simpleDateFormat.format(date);
    }


    public static List<String> getList() {
        List<String> list = new ArrayList<>();
        list.add("1");
        list.add("2");
        list.add("3");
        list.add("4");
        return list;
    }


    public static List<TypeSelectBean> getTypeList() {
        List<TypeSelectBean> list = new ArrayList<>();
        list.add(new TypeSelectBean("1","启停机",true));
        list.add(new TypeSelectBean("2","资产态变更",true));
        list.add(new TypeSelectBean("3","物联告警",true));
        list.add(new TypeSelectBean("4","维修保养",true));
        list.add(new TypeSelectBean("5","故障维修",true));
        list.add(new TypeSelectBean("6","其他运维",true));
        list.add(new TypeSelectBean("7","巡检正常",false));
        list.add(new TypeSelectBean("8","巡检异常",true));
        list.add(new TypeSelectBean("9","设备恢复",true));
        return list;
    }

    private static Context context;

    /**
     * 初始化工具类
     *
     * @param context 上下文
     */
    public static void init(@NonNull final Context context) {
        Utils.context = context.getApplicationContext();
    }

    /**
     * 获取ApplicationContext
     *
     * @return ApplicationContext
     */
    public static Context getContext() {
        if (context != null) {
            return context;
        }
        throw new NullPointerException("should be initialized in application");
    }


    /**
     * 判定文件夹是否存在，不存在则创建
     *
     * @param directory
     * @return
     */
    public static boolean fileExits(String directory) {
        File file = new File(directory);
        if (file.exists()) {
            return true;
        } else {
            return file.mkdirs();
        }
    }


    public static boolean isEmpty(final Collection<?> coll) {
        return coll == null || coll.isEmpty();
    }


    public static String getNowTimeDHM(Long time) {
        long expirationTime = time;
        //获得当前时间戳
        long timeStamp = System.currentTimeMillis();
        //格式
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //转换为String类型
        String endDate = formatter.format(expirationTime);//结束的时间戳
        String startDate = formatter.format(timeStamp);//开始的时间戳
        // 获取服务器返回的时间戳 转换成"yyyy-MM-dd HH:mm:ss"
        // 计算的时间差
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String timeString="";
        try {
            Date d1 = df.parse(endDate);//后的时间
            Date d2 = df.parse(startDate); //前的时间
            Long diff = d1.getTime() - d2.getTime(); //两时间差，精确到毫秒
            Long day = diff / (1000 * 60 * 60 * 24); //以天数为单位取整
            Long hour = (diff / (60 * 60 * 1000) - day * 24); //以小时为单位取整
            Long min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60); //以分钟为单位取整
            Long second = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);//秒

//            String days = getContext().getResources().getString(R.string.days);
//            String hours = getContext().getResources().getString(R.string.hour);
//            String minutes = getContext().getResources().getString(R.string.minutes);
             timeString = day + "," + hour + "," + min + ",";


        } catch (
                ParseException e) {
            e.printStackTrace();
        }
        return timeString;
    }


    public static String getTimeYMDHMS(long timestamp){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateStr = sdf.format(new Date(timestamp));
        return dateStr;
    }


    public static String getTimeMDHMS(long timestamp){
        SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日 HH:mm:ss");
        String dateStr = sdf.format(new Date(timestamp));
        return dateStr;
    }

    public static String getTimeM_DHMS(long timestamp){
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm:ss");
        String dateStr = sdf.format(new Date(timestamp));
        return dateStr;
    }


    public static String getTimeMDHM(long timestamp){
        SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日 HH:mm");
        String dateStr = sdf.format(new Date(timestamp));
        return dateStr;
    }

    public static String getTimeM_DHM(long timestamp){
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm");
        String dateStr = sdf.format(new Date(timestamp));
        return dateStr;
    }

    public static String getTimeYMDHM(long timestamp){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String dateStr = sdf.format(new Date(timestamp));
        return dateStr;
    }
    public static String getTimeHM(long timestamp){
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        String dateStr = sdf.format(new Date(timestamp));
        return dateStr;
    }

}
